package com.parking.platform.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.common.core.model.StateData;
import com.parking.platform.dao.PlatformAccountMapper;
import com.parking.platform.dao.PlatformResourceMapper;
import com.parking.platform.dao.PlatformRoleMapper;
import com.parking.platform.dao.PlatformRoleResourceMapper;
import com.parking.platform.dto.ResourceDto;
import com.parking.platform.entity.PlatformResource;
import com.parking.platform.entity.PlatformRole;
import com.parking.platform.entity.PlatformRoleResource;
import com.parking.platform.exception.PlatformException;
import com.parking.platform.exception.PlatformExceptionCode;
import com.parking.platform.form.UpRoleResourceForm;
import com.parking.platform.service.PlatformResourceService;
import com.parking.platform.service.PlatformRoleResourceService;
import javafx.application.Platform;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@Service
public class PlatformRoleResourceServiceImpl extends ServiceImpl<PlatformRoleResourceMapper, PlatformRoleResource> implements PlatformRoleResourceService {

    @Resource
    private PlatformRoleResourceMapper platformRoleResourceMapper;

    @Resource
    private PlatformAccountMapper platformAccountMapper;
    @Resource
    private PlatformRoleMapper platformRoleMapper;
    @Resource
    private PlatformResourceMapper platformResourceMapper;

    @Resource
    private PlatformResourceServiceImpl platformResourceServiceImpl;

    /**
     * 修改管理员权限
     * 修改参数：
     * platformRoleResourceId:平台角色资源id
     * roleId:平台角色id
     * platformResourceId:平台资源id
     *
     * */
    @Override
    @Transactional
    public void modifyRoleResourceData(List<UpRoleResourceForm> form, Integer adminId) {
        System.out.println("参数"+form);

        List<Integer> resouId = form.stream().map(UpRoleResourceForm::getPlatformResourceId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(resouId)){
            QueryWrapper<PlatformResource> wrapper = new QueryWrapper<>();
            wrapper.in("platform_resource_id",resouId);
            List<PlatformResource> list = platformResourceMapper.selectList(wrapper);
            List<Integer> parentId = list.stream().filter(x -> x.getPlatformResourceParent() != 0).map(PlatformResource::getPlatformResourceParent).distinct().collect(Collectors.toList());
            List<Integer> children = list.stream().filter(x -> x.getPlatformResourceParent() != 0).map(PlatformResource::getPlatformResourceId).distinct().collect(Collectors.toList());
            children.addAll(parentId);
            Integer roleId = form.get(0).getId();
            QueryWrapper<PlatformRoleResource> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("platform_role_id",roleId);
            List<PlatformRoleResource> resources = platformRoleResourceMapper.selectList(queryWrapper);
            List<Integer> roleResourceIds = resources.stream().map(PlatformRoleResource::getPlatformResourceId).collect(Collectors.toList());
            List<Integer> differentElements = Stream.concat(
                    children.stream().filter(e -> !roleResourceIds.contains(e)),
                    roleResourceIds.stream().filter(e -> !children.contains(e))
            ).collect(Collectors.toList());
            for (Integer differentElement : differentElements) {
                if (children.contains(differentElement)){
                    PlatformRoleResource resource = new PlatformRoleResource();
                    resource.setPlatformRoleId(roleId);
                    resource.setPlatformResourceId(differentElement);
                    platformRoleResourceMapper.insert(resource);
                }else {
                    QueryWrapper<PlatformRoleResource> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("platform_role_id",roleId);
                    wrapper1.eq("platform_resource_id",differentElement);
                    platformRoleResourceMapper.delete(wrapper1);
                }
            }
        }

    }
}
